{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f3b20704-0fba-4615-a370-72ac5609c633",
   "metadata": {},
   "source": [
    "**制作cls_train.txt**  \n",
    "datasets_path为人名文件夹路径  \n",
    "前三模块只有路径和生成文件名区别  \n",
    "最后一个模块是追加测试集  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "c209fdcb-7524-4437-a1eb-b6a7f4ee7956",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "64de21f8-edb7-4c09-bfb1-ffdf515c4cb1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#---------------------#\n",
    "#   annotation_full\n",
    "#---------------------#\n",
    "datasets_path   = \"lfw_processed\"\n",
    "types_tmp      = os.listdir(datasets_path)\n",
    "types_name      = sorted(types_tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "98ddae10-be31-4b0b-b980-eddc0b707789",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Atal_Bihari_Vajpayee\n"
     ]
    }
   ],
   "source": [
    "print(type(types_tmp))\n",
    "print(len(types_tmp))\n",
    "print(types_tmp[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "55423475-29c3-4648-b820-56f2eb215db4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Abdullah_Gul\n"
     ]
    }
   ],
   "source": [
    "print(type(types_name))\n",
    "print(len(types_name))\n",
    "print(types_name[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ff247cdf-270d-4f8c-8ceb-6dd4e335e566",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_file = open('cls_train_full.txt', 'w')\n",
    "for cls_id, type_name in enumerate(types_name):\n",
    "    photos_path = os.path.join(datasets_path, type_name)\n",
    "    if not os.path.isdir(photos_path):\n",
    "        continue\n",
    "    # photos_name=['001.jpg','002.jpg',……]\n",
    "    photos_name = os.listdir(photos_path)\n",
    "\n",
    "    for photo_name in photos_name:\n",
    "        if photo_name.split('.')[-1] == 'jpg':\n",
    "            # photo_name='001.jpg'\n",
    "            # cls_id;img_path\n",
    "            list_file.write(str(cls_id) + \";\" + '%s'%(os.path.join(os.path.abspath(datasets_path), type_name, photo_name)))\n",
    "            list_file.write('\\n')\n",
    "list_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "290cfb12-eb4e-4472-a418-5e1449e96f67",
   "metadata": {},
   "source": [
    "---------------------------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "be191dde-bb2b-4c98-906d-17028466e1e1",
   "metadata": {},
   "outputs": [],
   "source": [
    "#---------------------#\n",
    "#   annotation\n",
    "#---------------------#\n",
    "datasets_path   = \"dataset/train\"\n",
    "types_tmp      = os.listdir(datasets_path)\n",
    "types_name      = sorted(types_tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "102b5473-acff-40bf-96dc-9db7bc1a33c2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Naji_Sabri\n"
     ]
    }
   ],
   "source": [
    "print(type(types_tmp))\n",
    "print(len(types_tmp))\n",
    "print(types_tmp[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fe601df8-2f70-4c2f-8f37-8399675749b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Abdullah_Gul\n"
     ]
    }
   ],
   "source": [
    "print(type(types_name))\n",
    "print(len(types_name))\n",
    "print(types_name[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "a0bc3d62-c7fd-4233-ae0e-4d5f91b3526b",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_file = open('cls_train.txt', 'w')\n",
    "for cls_id, type_name in enumerate(types_name):\n",
    "    photos_path = os.path.join(datasets_path, type_name)\n",
    "    if not os.path.isdir(photos_path):\n",
    "        continue\n",
    "    # photos_name=['001.jpg','002.jpg',……]\n",
    "    photos_name = os.listdir(photos_path)\n",
    "\n",
    "    for photo_name in photos_name:\n",
    "        if photo_name.split('.')[-1] == 'jpg':\n",
    "            # photo_name='001.jpg'\n",
    "            # cls_id;img_path\n",
    "            list_file.write(str(cls_id) + \";\" + '%s'%(os.path.join(os.path.abspath(datasets_path), type_name, photo_name)))\n",
    "            list_file.write('\\n')\n",
    "list_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "669fa181-daaf-410a-af82-ede951920c57",
   "metadata": {},
   "source": [
    "-----------------------------------------------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "e03b6200-633e-4240-9046-0336b7e23947",
   "metadata": {},
   "outputs": [],
   "source": [
    "#---------------------#\n",
    "#   annotation_crop\n",
    "#---------------------#\n",
    "datasets_path   = \"lfwc_processed/train\"\n",
    "types_tmp       = os.listdir(datasets_path)\n",
    "types_name      = sorted(types_tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "059ecf9c-ffbd-49e2-92d2-6926502d8ba8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Al_Gore\n"
     ]
    }
   ],
   "source": [
    "print(type(types_tmp))\n",
    "print(len(types_tmp))\n",
    "print(types_tmp[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "17f0ff9e-b07e-40ec-b711-576cf2f34d93",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Abdullah_Gul\n"
     ]
    }
   ],
   "source": [
    "print(type(types_name))\n",
    "print(len(types_name))\n",
    "print(types_name[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "ab0c88bb-295b-4267-865b-a64babac7e5b",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_file = open('cls_train_crop.txt', 'w')\n",
    "for cls_id, type_name in enumerate(types_name):\n",
    "    photos_path = os.path.join(datasets_path, type_name)\n",
    "    if not os.path.isdir(photos_path):\n",
    "        continue\n",
    "    # photos_name=['001.jpg','002.jpg',……]\n",
    "    photos_name = os.listdir(photos_path)\n",
    "\n",
    "    for photo_name in photos_name:\n",
    "        if photo_name.split('.')[-1] == 'jpg':\n",
    "            # photo_name='001.jpg'\n",
    "            # cls_id;img_path\n",
    "            list_file.write(str(cls_id) + \";\" + '%s'%(os.path.join(os.path.abspath(datasets_path), type_name, photo_name)))\n",
    "            list_file.write('\\n')\n",
    "list_file.close()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5ad2afdb-9d9e-45bf-b2d4-956af598f583",
   "metadata": {},
   "source": [
    "----------------------------------------------------------------------------------------------------------------------------"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "81c61cfe-8b2d-49da-b23f-50dcc2437403",
   "metadata": {},
   "outputs": [],
   "source": [
    "#---------------------#\n",
    "#   add_test\n",
    "#---------------------#\n",
    "datasets_path   = \"dataset/test\"\n",
    "types_tmp      = os.listdir(datasets_path)\n",
    "types_name      = sorted(types_tmp)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "657816dd-2378-482c-8b52-ed658e1fe642",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Sergio_Vieira_De_Mello\n"
     ]
    }
   ],
   "source": [
    "print(type(types_tmp))\n",
    "print(len(types_tmp))\n",
    "print(types_tmp[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "657f77c3-c67f-42a9-9f08-0ce9a2f56c0c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'list'>\n",
      "311\n",
      "Abdullah_Gul\n"
     ]
    }
   ],
   "source": [
    "print(type(types_name))\n",
    "print(len(types_name))\n",
    "print(types_name[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "64e48fba-8ed9-46bc-a01b-ad3390d65a9d",
   "metadata": {},
   "outputs": [],
   "source": [
    "list_file = open('cls_train.txt', 'a')\n",
    "for cls_id, type_name in enumerate(types_name):\n",
    "    photos_path = os.path.join(datasets_path, type_name)\n",
    "    if not os.path.isdir(photos_path):\n",
    "        continue\n",
    "    photos_name = os.listdir(photos_path)\n",
    "\n",
    "    for photo_name in photos_name:\n",
    "        if photo_name.split('.')[-1] == 'jpg':\n",
    "            # photo_name='001.jpg'\n",
    "            # cls_id;img_path\n",
    "            list_file.write(str(cls_id) + \";\" + '%s'%(os.path.join(os.path.abspath(datasets_path), type_name, photo_name)))\n",
    "            list_file.write('\\n')\n",
    "list_file.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.6.9 64-bit",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.9"
  },
  "vscode": {
   "interpreter": {
    "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
